package com.CuiChangHe.studentmanager.service.studentleaveinfo.impl;

import com.CuiChangHe.studentmanager.dao.StudentLeaveInfoDao;
import com.CuiChangHe.studentmanager.entity.studentleave.StudentLeaveInfoData;
import com.CuiChangHe.studentmanager.entity.studentleave.StudentLeaveInfoReturnMsg;
import com.CuiChangHe.studentmanager.mapper.StudentLeaveInfoMapper;
import com.CuiChangHe.studentmanager.service.studentleaveinfo.StudentLeaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;

/**
 * @Description :学生请销假业务逻辑实现
 * @Author :
 * @Date : 2021/11/19
 */
@Service
@Slf4j
public class StudentLeaveServiceImpl implements StudentLeaveService {

    private StudentLeaveInfoMapper mapper;

    @Autowired
    public void setMapper(StudentLeaveInfoMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    @Transactional(readOnly = true)
    public StudentLeaveInfoReturnMsg getAllLeaveInfo() {
        StudentLeaveInfoReturnMsg returnMsg = new StudentLeaveInfoReturnMsg();
        log.info("开始查找所有请假信息.");
        StudentLeaveInfoDao[] allStudentLeaveInfo = mapper.findAllStudentLeaveInfo();
        if (allStudentLeaveInfo == null) {
            log.warn("查找所有请假信息失败!返回为空!");
            returnMsg.setCode(50000);
            returnMsg.setMessage("查找所有请假信息失败!");
            return returnMsg;
        }
        log.info("查找结果为:{}", Arrays.toString(allStudentLeaveInfo));
        returnMsg.setCode(20000);
        returnMsg.setData(new StudentLeaveInfoData(allStudentLeaveInfo.length, allStudentLeaveInfo));
        return returnMsg;
    }

    @Override
    @Transactional(readOnly = true)
    public StudentLeaveInfoReturnMsg getAllNon_CheckedLeaveInfo() {
        StudentLeaveInfoReturnMsg returnMsg = new StudentLeaveInfoReturnMsg();
        log.info("开始查找所有未审批请假信息.");
        StudentLeaveInfoDao[] allNon_checkedStudentLeaveInfo = mapper.findAllNon_CheckedStudentLeaveInfo();
        if (allNon_checkedStudentLeaveInfo == null) {
            log.warn("查找所有未审批请假信息失败!返回为空!");
            returnMsg.setCode(50000);
            returnMsg.setMessage("查找所有未审批请假信息失败!");
            return returnMsg;
        }
        log.info("查找结果为:{}",Arrays.toString(allNon_checkedStudentLeaveInfo));
        returnMsg.setCode(20000);
        returnMsg.setData(new StudentLeaveInfoData(allNon_checkedStudentLeaveInfo.length, allNon_checkedStudentLeaveInfo));
        return returnMsg;
    }

    @Override
    @Transactional(readOnly = true)
    public StudentLeaveInfoReturnMsg getAllNon_ReportLeaveInfo() {
        StudentLeaveInfoReturnMsg returnMsg = new StudentLeaveInfoReturnMsg();
        log.info("开始查找所有未销假请假信息.");
        StudentLeaveInfoDao[] allNon_reportStudentLeaveInfo = mapper.findAllNon_ReportStudentLeaveInfo();
        if (allNon_reportStudentLeaveInfo == null) {
            log.warn("查找所有未销假请假信息失败!返回为空!");
            returnMsg.setCode(50000);
            returnMsg.setMessage("查找所有未销假请假信息失败!");
            return returnMsg;
        }
        log.info("查找结果为:{}",Arrays.toString(allNon_reportStudentLeaveInfo));
        returnMsg.setCode(20000);
        returnMsg.setData(new StudentLeaveInfoData(allNon_reportStudentLeaveInfo.length, allNon_reportStudentLeaveInfo));
        return returnMsg;
    }

    @Override
    @Transactional(readOnly = true)
    public StudentLeaveInfoReturnMsg getAllFinishInfo() {
        StudentLeaveInfoReturnMsg returnMsg = new StudentLeaveInfoReturnMsg();
        log.info("开始查找所有已销假信息.");
        StudentLeaveInfoDao[] allFinishStudentLeaveInfo = mapper.findAllFinishStudentLeaveInfo();
        if (allFinishStudentLeaveInfo == null) {
            log.warn("查找所有已销假信息失败!返回为空!");
            returnMsg.setCode(50000);
            returnMsg.setMessage("查找所有已销假信息失败!");
            return returnMsg;
        }
        log.info("查找结果为:{}", Arrays.toString(allFinishStudentLeaveInfo));
        returnMsg.setCode(20000);
        returnMsg.setData(new StudentLeaveInfoData(allFinishStudentLeaveInfo.length, allFinishStudentLeaveInfo));
        return returnMsg;
    }

    @Override
    @Transactional
    public StudentLeaveInfoReturnMsg addNewLeaveInfo(StudentLeaveInfoDao data) {
        StudentLeaveInfoReturnMsg returnMsg = new StudentLeaveInfoReturnMsg();
        log.info("开始新增请假信息.data:{}", data);
        if (data == null) {
            log.warn("请假信息为空!");
            returnMsg.setCode(50000);
            returnMsg.setMessage("请假信息为空!");
            return returnMsg;
        }
        if (data.getStudentid() == null || data.getStudentid().equals("")) {
            log.warn("未找到data中包含学号信息,添加失败!data:{}", data);
            returnMsg.setCode(50000);
            returnMsg.setMessage("未找到学号信息,请假信息申请失败!");
            return returnMsg;
        }
        data.setIs_non_checked("true");
        data.setIs_non_report("true");
        data.setStatus("未审批");
        data.setIdentifier(Long.toString(new Date().getTime()));
        log.info("请假信息处理完毕.开始上库.data:{}", data);
        Boolean aBoolean = mapper.addNewLeaveInfo(data);
        if (!aBoolean) {
            log.warn("请假信息上库失败!data:{}", data);
            returnMsg.setCode(50000);
            returnMsg.setMessage("添加请假信息失败!");
            return returnMsg;
        }
        log.info("请假信息上库成功!本次业务结束!");
        returnMsg.setCode(20000);
        returnMsg.setMessage("请假信息添加成功!");
        return returnMsg;
    }

    @Override
    @Transactional
    public StudentLeaveInfoReturnMsg CheckAgree(String identifier) {
        StudentLeaveInfoReturnMsg returnMsg = new StudentLeaveInfoReturnMsg();
        log.info("开始通过identifier审批请假信息.identifier:{}", identifier);
        StudentLeaveInfoDao studentLeaveInfoByIdentifier = mapper.findStudentLeaveInfoByIdentifier(identifier);
        if (studentLeaveInfoByIdentifier == null) {
            log.warn("未能通过identifier查找到请假信息!identifier:{}", identifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("未能通过编号找到请假信息!");
            return returnMsg;
        }
        log.info("已找到请假信息.result:{}", studentLeaveInfoByIdentifier);
        if (studentLeaveInfoByIdentifier.getIs_non_checked().equals("false")) {
            log.warn("该请假信息已经审批!result:{}", studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("该请假信息已审批!");
            return returnMsg;
        }
        log.info("信息验证通过.即将修改审批结果.");
        Boolean check = mapper.Check(identifier);
        Boolean status = mapper.updateStudentLeaveInfoStatusByIdentifier(identifier, "已请假");
        if (!check) {
            log.warn("修改审批结果失败!result:{}", studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("通过审批失败!");
            return returnMsg;
        }
        if(!status){
            log.warn("修改状态失败!result:{}",studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("修改状态失败!");
            return returnMsg;
        }
        log.info("修改审批结果过成功.本次业务结束.");
        returnMsg.setCode(20000);
        returnMsg.setMessage("审批成功!");
        return returnMsg;
    }

    @Override
    @Transactional
    public StudentLeaveInfoReturnMsg CheckRefuse(String identifier) {
        StudentLeaveInfoReturnMsg returnMsg = new StudentLeaveInfoReturnMsg();
        log.info("开始通过identifier拒绝审批请假信息.identifier:{}", identifier);
        StudentLeaveInfoDao studentLeaveInfoByIdentifier = mapper.findStudentLeaveInfoByIdentifier(identifier);
        if (studentLeaveInfoByIdentifier == null) {
            log.warn("未能通过identifier查找到请假信息!identifier:{}", identifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("未能通过编号找到请假信息!");
            return returnMsg;
        }
        log.info("已找到请假信息.result:{}", studentLeaveInfoByIdentifier);
        if (studentLeaveInfoByIdentifier.getIs_non_checked().equals("false")) {
            log.warn("该请假信息已经审批!result:{}", studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("该请假信息已审批!");
            return returnMsg;
        }
        log.info("信息验证通过.即将修改审批结果.");
        Boolean check = mapper.Check(identifier);
        Boolean status = mapper.updateStudentLeaveInfoStatusByIdentifier(identifier, "已拒绝");
        if (!check) {
            log.warn("修改审批结果失败!result:{}", studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("拒绝审批失败!");
            return returnMsg;
        }
        if(!status){
            log.warn("修改状态失败!result:{}",studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("修改状态失败!");
            return returnMsg;
        }
        log.info("修改审批结果过成功.本次业务结束.");
        returnMsg.setCode(20000);
        returnMsg.setMessage("拒绝成功!");
        return returnMsg;
    }

    @Override
    @Transactional
    public StudentLeaveInfoReturnMsg Report(String identifier) {
        StudentLeaveInfoReturnMsg returnMsg = new StudentLeaveInfoReturnMsg();
        log.info("开始通过identifier销假.identifier:{}", identifier);
        StudentLeaveInfoDao studentLeaveInfoByIdentifier = mapper.findStudentLeaveInfoByIdentifier(identifier);
        if (studentLeaveInfoByIdentifier == null) {
            log.warn("未能通过identifier查找到请假信息!identifier:{}", identifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("未能通过编号找到请假信息!");
            return returnMsg;
        }
        log.info("已找到请假信息.result:{}", studentLeaveInfoByIdentifier);
        if (studentLeaveInfoByIdentifier.getIs_non_report().equals("false")) {
            log.warn("该请假信息已经销假!result:{}", studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("该请假信息已销假!");
            return returnMsg;
        }
        log.info("信息验证通过.即将修改销假结果.");
        Boolean report = mapper.Report(identifier);
        Boolean status = mapper.updateStudentLeaveInfoStatusByIdentifier(identifier, "已销假");
        if (!report) {
            log.warn("修改销假结果失败!result:{}", studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("销假失败!");
            return returnMsg;
        }
        if(!status){
            log.warn("修改状态失败!result:{}",studentLeaveInfoByIdentifier);
            returnMsg.setCode(50000);
            returnMsg.setMessage("修改状态失败!");
            return returnMsg;
        }
        log.info("修改销假结果过成功.本次业务结束.");
        returnMsg.setCode(20000);
        returnMsg.setMessage("销假成功!");
        return returnMsg;
    }
}
